package com.lohit.dbtest;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class ScheduleAppActivity extends ListActivity {

	DbController controller = new DbController(this);

	static final int DATE_DIALOG_ID = 1;

	String date;

	

	TextView headingDate;
	ListView appList;

	Button st;

	Button b1;

	static final int TIME_DIALOG_ID = 0;
	Button createBtn, cancelBtn;
	int mYear, mMonth, mDay;
	int hour, minute;

	ArrayList<AppointmentDetails> details = new ArrayList<AppointmentDetails>();

	@SuppressLint("SimpleDateFormat")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);


		setContentView(R.layout.activity_schedule);

		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);

		Date d1 = new Date();

		SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");

		date = df.format(d1);

		headingDate = (TextView) findViewById(R.id.dateHeadingTextView);

		appList = getListView();

		
		details = controller.getAppointmentList(date);



		appList.setAdapter(new CustomAdapter(details, this));


	}

	public void message(String name) {

		Toast.makeText(this, name, Toast.LENGTH_SHORT).show();

	}

	public void gotoMenu(View v) {

		Intent i = new Intent(this, MenuActivity.class);

		startActivity(i);

	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {

	

		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
					mDay);

		}

		return null;
		
		
	}

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {

			mYear = year;
			mMonth = monthOfYear + 1;
			mDay = dayOfMonth;

			String date = pad(mDay) + "/" + pad(mMonth) + "/" + pad(mYear);
			
			Log.d("date", date);

			Log.d("dateOfAppointment", date);
			headingDate.setText("Appointment on " + pad(mDay) + "/"
					+ pad(mMonth) + "/" + mYear);
		
			details.clear();

			details = controller.getAppointmentList(date);
			
			appList.setAdapter(new CustomAdapter(details,ScheduleAppActivity.this));

		}
	};

	public static String pad(int c) {
		String cc;
		if (c < 10) {

			cc = "0" + c;

			return cc;

		} else {

			return String.valueOf(c);
		}

	}

	@Override
	protected void onResume() {

		super.onResume();

		st = (Button) findViewById(R.id.chooseDateButton);

		st.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub

				showDialog(DATE_DIALOG_ID);

				return true;

			}
		});

	}

}
